import 'package:flutter/material.dart';

import 'package:junyeyishu/utils/utils.dart';

class QGSaleRecordCell extends StatelessWidget {
  Map data;
  QGSaleRecordCell({super.key, required this.data});

  @override
  Widget build(BuildContext context) {
    bool isSaled = data["own_num"] > 0;

    return GestureDetector(
      onTap: () {
        if (!isSaled) {
          toast("您没有此藏品");
          return;
        }
        toPurchase(context);
      },
      child: Container(
        color: Colors.transparent,
        padding: EdgeInsets.only(left: 15.rpx, right: 15.rpx, top: 20.rpx),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Flexible(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Text(
                            data["order_name"] ?? '',
                            style: TextStyle(
                                color: Color(0xFF111111),
                                fontSize: 14.rpx,
                                fontWeight: FontWeight.bold),
                          ),
                        ],
                      ),
                      SizedBox(height: 12.rpx),
                      Text(
                        "￥${data["total_fee"]}",
                        style: TextStyle(
                            color: const Color(0xFF111111),
                            fontSize: 16.rpx,
                            fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                ),
                buildButton("出售", true)
              ],
            ),
            SizedBox(height: 17.rpx),
            Container(height: 0.5.rpx, color: Color(0xFFEAEAEA)),
          ],
        ),
      ),
    );
  }

  buildButton(String name, bool enable) {
    return Container(
      width: 64.rpx,
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(vertical: 6.rpx),
      decoration: enable
          ? BoxDecoration(
              gradient: const LinearGradient(
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                colors: [Color(0xFFFFF25A), Color(0xFFDCFF72)],
              ),
              borderRadius: BorderRadius.circular(6.rpx),
            )
          : BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                colors: [
                  const Color(0xFFFFF25A).withOpacity(0.5),
                  const Color(0xFFDCFF72).withOpacity(0.5)
                ],
              ),
              borderRadius: BorderRadius.circular(6.rpx),
            ),
      child: Text(
        name,
        style: TextStyle(
            fontSize: 13.rpx,
            color: enable ? Color(0xFF111111) : Color(0xFF999999)),
      ),
    );
  }

  toPurchase(ctx) {
    if (!User.isLogin) {
      Navigator.of(ctx).pushNamed(PageLogin);
      return;
    }

    data["name"] = data["order_name"];
    data["order_price"] = data["total_fee"];
    Navigator.of(ctx).pushNamed(PagePurchaseConfirm, arguments: data);
  }
}
